package cn.gs.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Aspect
@ComponentScan("cn.gs")
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true) // 添加此参数强制使用CGLIB
public class LogAspect {

    //定义切点
    @Pointcut("execution(* cn.gs.service.*.*(..))")   //仅JDK代理使用  有接口
    //@Pointcut("execution(* cn.gs.service..*.*(..))")  //使用双点号表示匹配service包及其所有子包
    public void pointcut(){}

    //环绕通知
    @Around("pointcut()")
    public  Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知：进入方法");
        Object o = joinPoint.proceed();
        System.out.println("环绕通知：退出方法");
        return o;
    }
    @Before("pointcut()")
    public void doBefore() {
        System.out.println("===============前置通知============");
    }

    //后置通知
    @AfterReturning(pointcut = "pointcut()", returning = "result")
    public String doAfterReturning(String result) {
        System.out.println("===============后置通知============");
        System.out.println("========返回值=====："+result);
        return result;
    }

    @AfterThrowing(pointcut = "pointcut()", throwing = "e")
    public String doAfterThrowing(Exception e) {
        System.out.println("===============异常通知============");
        System.out.println("========异常=====："+e.getMessage());
        return e.getMessage();
    }

    @After("pointcut()")
    public void doAfter() {
        System.out.println("===============最终通知============");
    }
}
